AgentsPortlet = Class.extend({
	init: function()	{
		this.name = "AgentsPortlet";
	},
	
	onBegin: function()	{
		this.model = {};
	},

	run: function()	{
		var obj = this;
		this.onAjax('agent', 'get-agents', {}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = {};
				obj.model.agentInfo = ret.agent_info;
				obj.model.agentsBac = ret.agents_bac;
				obj.model.agentsTrung = ret.agents_trung;
				obj.model.agentsNam = ret.agents_nam;
				obj.getPortletPlaceholder().paintCanvas(obj.render());
			}
		});
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface);

AgentAddPortlet = Class.extend({
	init: function()	{
		this.name = "AgentAddPortlet";
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	},
	
	run: function()	{
		var obj = this;
		this.getPortletPlaceholder().paintCanvas(this.render());
	},
	
	onAgentAddSubmit: function()	{
		var name=this.requestForEffectiveResource('Name').val();
		if (name.trim() == '')	{
			alert('Bạn chưa nhập tên đại lý');
			return;
		}
		var city=this.requestForEffectiveResource('City').val();
		var region=this.requestForEffectiveResource('Region').val();
		var address=this.requestForEffectiveResource('Address').val();
		var phone=this.requestForEffectiveResource('Phone').val();
		
		var obj = this;
		var subject = SingletonFactory.getInstance(Subject);
		this.onAjax('admin', 'add-new-agent', {name: name, city: city, region: region, address: address, phone: phone}, 'POST', {
			'onSuccess': function(ret)	{
				alert('Thêm đại lý thành công');
				obj.requestForEffectiveResource('Name').val('');
				obj.requestForEffectiveResource('City').val('');
				obj.requestForEffectiveResource('Address').val('');
				obj.requestForEffectiveResource('Phone').val('');
				subject.notifyEvent('AgentsChanged');
			}
		});
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);


AgentEditPortlet = Class.extend({
	init: function()	{
		this.name = "AgentEditPortlet";
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	},
	
	run: function()	{
		this.loadAgentDetail();
	},
	
	loadAgentDetail: function()	{
		var obj = this;
		var id = this.getRequest().getParam('id');
		this.onAjax('agent', 'get-agent-detail', {id:id}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = ret;
				obj.getPortletPlaceholder().paintCanvas(obj.render());
				$('#AgentEditPortlet-Region').val(ret.region);
			}
		});
	},
	
	onAgentEditSubmit: function()	{
		var id = this.getRequest().getParam('id');
		var name=this.requestForEffectiveResource('Name').val();
		if (name.trim() == '')	{
			alert('Bạn chưa nhập tên đại lý');
			return;
		}
		var city=this.requestForEffectiveResource('City').val();
		var region=this.requestForEffectiveResource('Region').val();
		var address=this.requestForEffectiveResource('Address').val();
		var phone=this.requestForEffectiveResource('Phone').val();
		
		var obj = this;
		var subject = SingletonFactory.getInstance(Subject);
		this.onAjax('admin', 'update-agent', {id:id, name: name, city: city, region: region, address: address, phone: phone}, 'POST', {
			'onSuccess': function(ret)	{
				alert('Sửa đại lý thành công');
			}
		});
	}
	
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);

AgentsManagePortlet = Class.extend({
	init: function()	{
		this.name = "AgentsManagePortlet";
	},
	
	onBegin: function()	{
		this.registerObserver();
	},
	
	onEnd: function()	{
		this.unregisterObserver();
	},
	
	onReloadPage: function()	{
		this.run();
	},
	
	run: function()	{
		this.loadAgents();
	},
	
	loadAgents: function()	{
		var obj = this;
		this.onAjax('agent', 'get-agents', {}, 'GET', {
			'onSuccess': function(ret)	{
				obj.model = {};
				obj.model.agentInfo = ret.agent_info;
				obj.model.agentsBac = ret.agents_bac;
				obj.model.agentsTrung = ret.agents_trung;
				obj.model.agentsNam = ret.agents_nam;
				obj.getPortletPlaceholder().paintCanvas(obj.render());
			}
		});
	},
	
	onAgentsChanged: function()	{
		this.run();
	},
	
	onRegionChanged: function(eventData)	{
		var event = eventData.event.target;
		var id = eventData.id;
		this.onAjax('admin', 'update-agent', {id: id, region: $(event).val()}, 'POST', {});
	},
	
	
	onDeleteAgent: function(eventData)	{
		var event = eventData.event;
		var id = eventData.id;
		var obj = this;
		this.onAjax('admin', 'delete-agent', {id: id}, 'POST', {
			'onSuccess': function(ret)	{
				obj.run();
			}
		});
	}
}).implement(PortletInterface).implement(RenderInterface).implement(AjaxInterface).implement(ObserverInterface);